/**
 * Created by chesw on 2017/5/12.
 * describe：我的就诊人
 */
var myDiagnosis = Vue.extend({
    props: [],
    data: function () {
        return {
            showAdd: false,
            addBtn: 'add-btn',
            reduceBtn: 'reduce-btn',
            df: 'default',
            dfBtn: 'default-btn',
            p_obj: {
                url: 'logo',
                text: '提示文字'
            },
            PatientVisitorInfo: {
                visitID: '',
                editName: '',
                editPhone: '',
                editCode: '',
                editCard: '',
                editDefault: false,
                isUpdate: false,
                fromSetDefault: false
            },
            title: '新增就诊人',
            saveDisable: true, //保存按钮是否可点击
            ValidateCodeDisable: true,
            errInfo: {
                type: -1, //-1：默认，1：用户名，2：手机号码，3：验证码，4：证件号码
                info: ''
            },
            pageInfo: {
                allRecord: [],
                curPageRecord: [],
                totalCount: "",
                pageSize: 10,
                pageCount: 10,
                pageCurrent: 1
            },
            loginInfo: { isLogin: '', name: '', mobile: '', picUrl: '' },
            showCheckbox: true,
            mobileNotEditable: false,
            codeNotEditable: false,
            curPhoneNumber:''
        }
    },
    created: function () {
        var _self = this;
        _self.GetAllPatient(); 
        CheckLoginStatus(_self, _self.loginInfo, true, true);
    },
    methods: {
        ShowResultEmpty: function (p_show) {
            var resultEmpty = document.getElementById("result-empty");
            var pageBox = document.getElementById("clinic-page-box");
            if (p_show) {
                resultEmpty.style.display = "none";
            }
            else {
                pageBox.style.display = "none";
            }
        },
        GetAllPatient: function () {
            var _self = this;
            _self.pageInfo.allRecord = [];
            _self.$http.post("/PersonalCenter/GetPatientVisitList").then(function (res) {
                if (res.body.length > 0) {
                    for (var i = 0; i < res.body.length; i++) {
                        _self.pageInfo.allRecord.push(res.body[i]);
                    }
                    _self.pageInfo.totalCount = _self.pageInfo.allRecord.length;
                    _self.pageInfo = showPageRecords(1, _self.pageInfo);
                    _self.showPage();
                    _self.ShowResultEmpty(true);
                }
                else {
                    _self.ShowResultEmpty(false);
                }               
            });
        },
        showPage: function () {
            var _self = this;
            var pageBox = document.getElementById("clinic-page-box");

            if (_self.pageInfo.pageCount === 1)
                pageBox.style.display = "none";
            else
                pageBox.style.display = "block";

            var param = {
                pageCount: _self.pageInfo.pageCount,
                curPage: _self.pageInfo.pageCurrent,
                pageInfo:_self.pageInfo,
                page_func: function (p_curPage,p_pageInfo) {
                    showPageRecords(p_curPage, p_pageInfo);
                }                
            };
            _self.$emit('show', param);
        },
        clickAdd: function (p_type, p_index) {
            var _this = this;
            _this.showAdd = !_this.showAdd;                
            _this.clearError();
            _this.mobileNotEditable = false;

            if (p_type === 2)
                _this.PatientVisitorInfo.isUpdate = true;         
                       
            if (_this.PatientVisitorInfo.isUpdate) {
                _this.title = '编辑就诊人';
                _this.codeNotEditable = true;
                _this.showCheckbox = true;
                if (p_index != null) {
                    _this.PatientVisitorInfo.visitID = _this.pageInfo.curPageRecord[p_index].Id;
                    _this.PatientVisitorInfo.editName = _this.pageInfo.curPageRecord[p_index].FamilyName;
                    _this.PatientVisitorInfo.editPhone = _this.pageInfo.curPageRecord[p_index].FamilyMobile;
                    _this.PatientVisitorInfo.editCard = _this.pageInfo.curPageRecord[p_index].IDCardNo;
                    _this.PatientVisitorInfo.editDefault = _this.pageInfo.curPageRecord[p_index].IsDefault;                  
                    _this.curPhoneNumber = _this.pageInfo.curPageRecord[p_index].FamilyMobile;
                    _this.checkValue();

                    if (_this.pageInfo.curPageRecord[p_index].IsDefault)
                        _this.showCheckbox = false;

                    if (_this.pageInfo.curPageRecord[p_index].FamilyName === _this.loginInfo.name && 
                        _this.pageInfo.curPageRecord[p_index].FamilyMobile === _this.loginInfo.mobile)
                        _this.mobileNotEditable = true;

                    if (!_this.showAdd) {
                        _this.clearInfo();
                    } else {
                        _this.ValidateCodeDisable = true;
                    }
                }
                else {
                    if (_this.PatientVisitorInfo.editDefault)
                        _this.showCheckbox = false;

                    if (_this.PatientVisitorInfo.editName === _this.loginInfo.name) 
                        _this.mobileNotEditable = true;
                }

                
            } else {
                _this.title = '新增就诊人';
                //this.clearInfo();
            }
        },
        clearInfo: function () {
            this.title = '新增就诊人';
            this.errInfo.type = -1;
            this.errInfo.info = "";
            this.PatientVisitorInfo.editName = '';
            this.PatientVisitorInfo.editPhone = '';
            this.PatientVisitorInfo.editCode = '';
            this.PatientVisitorInfo.editCard = '';
            this.PatientVisitorInfo.editDefault = false;
            this.PatientVisitorInfo.isUpdate = false;
            this.codeNotEditable = false;
            this.showCheckbox = true;
            this.ValidateCodeDisable = true;
        },
        //disableGetValidateCode:function(p_disable){
        //    var GetValidateCode = document.getElementById("getValidateCode");
        //    if (p_disable) {
        //        this.ValidateCodeDisable = true;
        //    }
        //    else {
        //        if (GetValidateCode.innerText == "获取验证码")
        //            this.ValidateCodeDisable = false;
        //    }
        //},
        checkValue: function () {
            //实时验证输入值
            this.saveDisable = true;
            this.ValidateCodeDisable = true;
            var getValidateCode = document.getElementById("getValidateCode");
            if (!this.PatientVisitorInfo.isUpdate) {
                if (!isEmpty(this.PatientVisitorInfo.editPhone) && getValidateCode.innerText === "获取验证码") {
                    this.ValidateCodeDisable = false;
                }               
            }                
            else {
                this.codeNotEditable = true;              
                if (this.PatientVisitorInfo.editPhone != this.curPhoneNumber) {
                    if (this.PatientVisitorInfo.editCode === "000000")
                        this.PatientVisitorInfo.editCode = "";
                    this.ValidateCodeDisable = false;
                    this.codeNotEditable = false;
                }
                else {
                    this.PatientVisitorInfo.editCode = "000000";
                    this.ValidateCodeDisable = true;
                    this.codeNotEditable = true;
                }
                    
            }

            if (isEmpty(this.PatientVisitorInfo.editName)) {
                return false;
            }
            else if (isEmpty(this.PatientVisitorInfo.editPhone)) {
                return false;
            }
            else if(isEmpty(this.PatientVisitorInfo.editCode)) {
                return false;
            }
            else if (isEmpty(this.PatientVisitorInfo.editCard)) {
                return false;
            }
                     
            this.saveDisable = false;
        },
        diagnosisValidate: function () {
            //点击保存按钮，验证输入参数是否正确合法
            //用户名
            if (!isValidOfName(this.PatientVisitorInfo.editName, this.errInfo)) {
                return false;
            };
            //手机号码
            if (!isValidOfPhone(this.PatientVisitorInfo.editPhone, this.errInfo)) {
                return false;
            }
            //验证码
            if (!isValidOfCode(this.PatientVisitorInfo.editCode, this.errInfo)) {
                return false;
            }
            //证件号码
            if (!isValidOfCard(this.PatientVisitorInfo.editCard, this.errInfo)) {
                return false;
            }
         
            return true;
        },
        clearError:function(){
            this.errInfo.info = "";
        },
        save: function (p_showMsg,p_doValidate) {
            var _self = this;

            if (p_doValidate == null)
                p_doValidate = true;

            if (p_doValidate&&!this.diagnosisValidate()) {
                return;
            }
            if (_self.PatientVisitorInfo.isUpdate) {
                _self.$http.post("/PersonalCenter/UpdatePatientVisitor", _self.PatientVisitorInfo).then(function (res) {
                    if (res.body.Code === -1)
                        dialogTip({ url: '/styles/image/icon/no.png', text: res.body.Msg });                       
                    else {
                        _self.showAdd = false;                        
                        if (p_showMsg) {
                            _self.pageInfo.allRecord = [];
                            dialogTip({ url: '/styles/image/icon/ok.png', text: "更新成功" }); 
                            for (var i = 0; i < res.body.length; i++) {
                                _self.pageInfo.allRecord.push(res.body[i]);
                            }
                            _self.pageInfo.totalCount = _self.pageInfo.allRecord.length;
                            _self.pageInfo = showPageRecords(1, _self.pageInfo);
                            _self.showPage();
                            _self.clearInfo();
                        }                      
                    }
                    _self.GetAllPatient();
                    
                });
            }
            else {
                _self.$http.post("/PersonalCenter/AddPatientVisitor", _self.PatientVisitorInfo).then(function (res) {
                    if (res.body.Code === -1)
                        dialogTip({ url: '/styles/image/icon/no.png', text: res.body.Msg });
                    else {
                        _self.showAdd = false;
                        _self.pageInfo.allRecord = [];
                        dialogTip({ url: '/styles/image/icon/ok.png', text: "添加成功" });
                        if (res.body.length > 0) {
                            for (var i = 0; i < res.body.length; i++) {
                                _self.pageInfo.allRecord.push(res.body[i]);
                            }
                            _self.pageInfo.totalCount = _self.pageInfo.allRecord.length;
                            _self.pageInfo = showPageRecords(1, _self.pageInfo);
                            _self.showPage();
                            _self.ShowResultEmpty(true);
                        }
                        else {
                            _self.ShowResultEmpty(false);
                        }
                        _self.clearInfo();
                    }
                    _self.GetAllPatient();
                    
                });
            }
            
        },
        cancel: function () {
            this.showAdd = false;
            this.clearInfo();
        },
        del: function (p_index) {
            var _this = this;
            var param = {
                text: '<p class="message">该操作不可撤销，是否确认删除该就诊人</p>',
                l_val: '确认',
                r_val: '取消',
                type: true,
                showBtn: 2, //显示按钮个数， 0：不显示，1：显示一个按钮，2：显示两个
                left_func: function () {
                    _this.$parent.confirm.type = false;
                    _this.$http.post("/PersonalCenter/DelPatientVisitor", _this.pageInfo.curPageRecord[p_index]).then(function (res) {
                        if (res.body.Code === -1) {
                            dialogTip({ url: '/styles/image/icon/no.png', text: res.body.Msg });
                        }
                        else {
                            _this.pageInfo.allRecord = [];
                            dialogTip({ url: '/styles/image/icon/ok.png', text: "删除成功" });
                            if (res.body.length > 0) {
                                for (var i = 0; i < res.body.length; i++) {
                                    _this.pageInfo.allRecord.push(res.body[i]);
                                }
                                _this.pageInfo.totalCount = _this.pageInfo.allRecord.length;
                                _this.pageInfo = showPageRecords(1, _this.pageInfo);
                                _this.showPage();
                                _this.ShowResultEmpty(true);
                            }
                            else {
                                _this.pageInfo.curPageRecord.pop(_this.pageInfo.curPageRecord[p_index]);
                                _this.ShowResultEmpty(false);
                            }
                            
                        }  
                        _this.GetAllPatient();
                    });                  
                }
            };
            this.$emit('del', param);
        },
        setAsDefault: function (p_index) {
            var _self = this;
            if (!_self.pageInfo.curPageRecord[p_index].IsDefault) {
                for (var i = 0; i < _self.pageInfo.curPageRecord.length; i++) {
                    if (i !== p_index) {
                        if (_self.pageInfo.curPageRecord[i].IsDefault) {
                            _self.PatientVisitorInfo.visitID = _self.pageInfo.curPageRecord[i].Id;
                            _self.PatientVisitorInfo.editName = _self.pageInfo.curPageRecord[i].FamilyName;
                            _self.PatientVisitorInfo.editPhone = _self.pageInfo.curPageRecord[i].FamilyMobile;
                            _self.PatientVisitorInfo.editCard = _self.pageInfo.curPageRecord[i].IDCardNo;
                            _self.PatientVisitorInfo.editCode = "000000";
                            _self.PatientVisitorInfo.editDefault = false;
                            _self.PatientVisitorInfo.isUpdate = true;
                            _self.PatientVisitorInfo.fromSetDefault = true;
                            _self.save(false,false);
                        }                        
                    }
                    else {
                        _self.PatientVisitorInfo.visitID = _self.pageInfo.curPageRecord[p_index].Id;
                        _self.PatientVisitorInfo.editName = _self.pageInfo.curPageRecord[p_index].FamilyName;
                        _self.PatientVisitorInfo.editPhone = _self.pageInfo.curPageRecord[p_index].FamilyMobile;
                        _self.PatientVisitorInfo.editCard = _self.pageInfo.curPageRecord[p_index].IDCardNo;
                        _self.PatientVisitorInfo.editCode = "000000";
                        _self.PatientVisitorInfo.editDefault = true;
                        _self.PatientVisitorInfo.isUpdate = true;
                        _self.PatientVisitorInfo.fromSetDefault = true;
                        _self.save(true,false);
                    }
                    
                }
            }          
        },
        //获取验证码
        getValidateCode: function () {
            var _self = this;
            if (!isValidOfPhone(this.PatientVisitorInfo.editPhone, this.errInfo)) {
                return false;
            }
            _self.$http.post("/PersonalCenter/GetValidateCode", _self.PatientVisitorInfo).then(function (res) {
                if (res.body.Code === -1) {
                    _self.errInfo.type = 3;
                    _self.errInfo.info = res.body.Msg;
                } else {
                    _self.p_obj.url = "/styles/image/icon/ok.png";
                    _self.p_obj.text = res.body.Msg;
                    countDown('getValidateCode');
                    dialogTip(_self.p_obj);
                }
            }).catch(function (err) {
                _self.errInfo.type = 3;
                _self.errInfo.info = "获取验证码异常";
            });
        }
    },
    template: '<div class="my-diagnosis">' +
        '<div class="add-diagnosis">' +
            '<div class="btn-title" :class="[showAdd ? addBtn : reduceBtn]" @click="clickAdd()" id="titleBtn">{{title}}</div>' +
            '<div class="add-content" v-if="showAdd">' +
                '<div class="diagnosis-style flex">' +
                    '<span class="label-cls">就诊人姓名</span>' +
                    '<span class="input-cls normal-input-cls">' +
                        '<input class="normal-input" type="text" placeholder="就诊人名称" v-model="PatientVisitorInfo.editName" maxlength="16" v-on:input="checkValue">' +
                        '<i class="x-icon"></i>' +
                    '</span>' +
                    '<span class="required" id="editName">* {{errInfo.type==1? errInfo.info : ""}}</span>' +
                '</div>' +
                '<div class="diagnosis-style flex">' +
                    '<span class="label-cls">手机号</span>' +
                    '<span class="input-cls normal-input-cls">' +
                        '<input class="normal-input" type="tel" maxlength="11" placeholder="手机号" v-model="PatientVisitorInfo.editPhone" v-on:input="checkValue" :disabled="mobileNotEditable">' +
                        '<i class="x-icon"></i>' +
                    '</span>' +
                    '<span class="required" id="editPhone">* {{errInfo.type==2? errInfo.info : ""}}</span>' +
                '</div>' +
                '<div class="diagnosis-style flex" v-if="!codeNotEditable">' +
                    '<span class="label-cls">验证码</span>' +
                    '<span class="input-cls code-input-cls">' +
                        '<input class="code-input" type="tel" maxlength="6" placeholder="验证码" v-model="PatientVisitorInfo.editCode" v-on:input="checkValue">' +
                        '<i class="x-icon"></i>' +
                    '</span>' +
                    '<button class="code-btn" id="getValidateCode" @click ="getValidateCode" :disabled="ValidateCodeDisable">获取验证码</button>' +
                    '<span class="required" id="validationCode">* {{errInfo.type==3? errInfo.info : ""}}</span>' +
                '</div>' +
                '<div class="diagnosis-style flex">' +
                    '<span class="label-cls">证件号</span>' +
                    '<span class="input-cls normal-input-cls">' +
                        '<input class="normal-input" type="text" placeholder="证件号" v-model="PatientVisitorInfo.editCard" maxlength="18" v-on:input="checkValue">' +
                        '<i class="x-icon"></i>' +
                    '</span>' +
                    '<span class="required" id="editCard">* {{errInfo.type==4? errInfo.info : ""}}</span>' +
                '</div>' +
                '<div class="bottom-btn">' +
                    '<input style="display:none" type="text" v-model="PatientVisitorInfo.isUpdate">' +
                    '<span class="checkbox"><input type="checkbox" v-model="PatientVisitorInfo.editDefault" v-if="showCheckbox"><span v-if="showCheckbox">&nbsp;&nbsp;设置默认就诊人</span></span>' +
                    '<button class="btn save" @click="save(true)" :disabled="saveDisable">保存</button>' +
                    '<button class="btn cancel" @click="cancel">取消</button>' +
                '</div>' +
            '</div>' +
        '</div>' +
        '<div class="diagnosis-list">' +
            '<div class="md-title flex">' +
                '<p class="md-name">姓名</p>' +
                '<p class="md-sex">性别</p>' +
                '<p class="md-age">年龄</p>' +
                '<p class="md-phone">手机号</p>' +
                '<p class="md-id-card">身份证</p>' +
                '<p class="md-operate">操作</p>' +
                '<p class="md-set-default"></p>' +
            '</div>' +
            '<div class="md-content flex" :class="item.cls" v-for="(item, index) in pageInfo.curPageRecord">' +
                '<p class="md-name">{{item.FamilyName}}</p>' +
                '<p class="md-sex">{{item.Gender}}</p>' +
                '<p class="md-age">{{item.Age}}</p>' +
                '<p class="md-phone">{{item.FamilyMobile}}</p>' +
                '<p class="md-id-card">{{item.IDCardNo}}</p>' +
                '<p class="md-operate"><span @click="clickAdd(2, index)">修改&nbsp;</span>|<span @click="del(index)">&nbsp;删除</span></p>' +
                '<p class="md-set-default"><span :class="[item.IsDefault ? df : dfBtn]" @click="setAsDefault(index)">{{item.IsDefault ? "默认就诊人" : "设置默认"}}</span></p>' +
            '</div>' +
        '</div>' +
        '<div class="result-empty" id="result-empty">暂无数据</div>' +
    '</div>'
});